﻿using System;
using System.Collections;
using System.Data.SqlClient;
using com.fukefu.report;
using com.fukefu.sql;

namespace com.fukefu.template
{
    public partial class LabelForm : BaseForm
    {
        string currentId = null;

        SqlDataReader re;
        SqlServerOperate sql = new SqlServerOperate();
        Hashtable hs = new Hashtable();

        public LabelForm()
            : this(null)
        {

        }
        public LabelForm(string id)
        {
            InitializeComponent();
            currentId = id;
            Init();
            if (id != null)
            {
                LoadHtml();
                this.Text = "编辑-卖点录入";
            }
        }
        private void Init()
        {
            SqlDataReader re = sql.Query("select pid,word from 全局设置.dbo.[_mb_标签分类] where part=1 order by weights desc");
            while (re.Read())
            {
                hs.Add(re["word"], re["pid"]);
                TypeComboBox.Items.Add(re["word"]);
            }
            re.Close();
        }
        //预览文档
        private void Preview_Click(object sender, EventArgs e)
        {
            ShowForm(PreviewForm.GetInstall(htmlTextBox1.GetHtml()));
        }
        //加载制定id文档
        private void LoadHtml()
        {
            re = sql.Query("select cont,word from [_mb_标签] as a,[全局设置].dbo.[_mb_标签分类] as b where a.id=" + currentId + " and a.pid=b.pid");
            if (re.Read())
            {
                htmlTextBox1.SetHtml(re["cont"].ToString());
                TypeComboBox.Text = re["word"].ToString();
            }
            re.Close();
        }
        //新建片段
        private void New_Click(object sender, EventArgs e)
        {
            if (ShowCheckDialog("是否新建标题片段?"))
            {
                currentId = null;
                htmlTextBox1.SetHtml("");
                this.Text = "新建-卖点录入";
            }
        }
        //保存片段
        private void Save_Click(object sender, EventArgs e)
        {
            SaveTemp();
        }
        //保存
        private void SaveTemp()
        {
            if (TypeComboBox.Text.Equals(""))
            {
                ShowMessageDialog("请选择分类!");
                return;
            }
            string html = htmlTextBox1.GetHtml().Replace("'", "''");
            if (currentId == null)
            {
                sql.Update("insert into [_mb_标签](cont,pid,uid) values('" + html + "','" + hs[TypeComboBox.Text] + "','" + LoginUser.Uid + "')");
                currentId = sql.GetMaxId("select max(id) from [_mb_标签]").ToString();
            }
            else
            {
                sql.Update("update [_mb_标签] set audit=0,pid='" + hs[TypeComboBox.Text] + "',cont='" + html + "' where id=" + currentId);
            }
            ShowMessageDialog("保存成功\t");
            this.Text = "保存-卖点录入";
        }
        //今日统计
        private void Statistics_Click(object sender, EventArgs e)
        {
            string cmd = "select  全局设置.dbo.[_mb_标签分类].word as 名称,COUNT(1) as 数量 from _mb_标签,全局设置.dbo.[_mb_标签分类] where 全局设置.dbo.[_mb_标签分类].pid=_mb_标签.pid  and uid='" + LoginUser.Uid + "' and DATEDIFF(D,GETDATE(),dt)=0 group by 全局设置.dbo.[_mb_标签分类].word";
            new StatisticsForm("卖点录入统计", cmd).Show();
        }
        //查看列表
        private void ListToolStripButton_Click(object sender, EventArgs e)
        {
            ShowForm(ListForm.GetInstance("标签"));
        }
    }
}
